package com.stox.client.ui.javafx.chartingy.model.shape;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Point2D;
import javafx.scene.shape.Line;

import com.stox.client.ui.javafx.chartingy.model.chart.Chart;

public class HorizontalSegment extends Shape {

	private final ControlPoint start;
	private final ControlPoint end;
	private final ControlPoint movePoint;
	private final Line line = new Line();
	private boolean moving = false;
	private final ChangeListener<Number> listener = new ChangeListener<Number>() {
		@Override
		public void changed(final ObservableValue<? extends Number> observable, final Number oldValue, final Number newValue) {
			if (!moving) {
				moving = true;
				movePoint.setCenterX((start.getCenterX() + end.getCenterX()) / 2);
				moving = false;
			}
		}
	};

	public HorizontalSegment(final Chart<?, ?> chart, final double initialSceneX, final double initialSceneY) {
		super(chart);
		start = new ControlPoint(chart);
		end = new ControlPoint(chart);
		movePoint = new ControlPoint(chart);

		line.startYProperty().bindBidirectional(line.endYProperty());
		start.centerYProperty().bindBidirectional(line.endYProperty());
		end.centerYProperty().bindBidirectional(line.endYProperty());
		deletePoint.centerYProperty().bindBidirectional(line.endYProperty());
		movePoint.centerYProperty().bindBidirectional(line.endYProperty());

		start.centerXProperty().addListener(listener);
		end.centerXProperty().addListener(listener);
		movePoint.centerXProperty().addListener(new ChangeListener<Number>() {
			@Override
			public void changed(final ObservableValue<? extends Number> observable, final Number oldValue, final Number newValue) {
				if ((0 < oldValue.doubleValue()) && !moving) {
					moving = true;
					// System.out.println(oldValue + ":" + newValue + ":" + start.getCenterX() + ":" +
					// end.getCenterX());
					double delta = oldValue.doubleValue() - start.getCenterX();
					start.setCenterX(newValue.doubleValue() - delta);
					delta = end.getCenterX() - oldValue.doubleValue();
					end.setCenterX(newValue.doubleValue() + delta);
					moving = false;
				}
			}
		});

		line.startXProperty().bind(start.centerXProperty());
		line.endXProperty().bind(end.centerXProperty());
		deletePoint.centerXProperty().bind(line.startXProperty().add(line.endXProperty().subtract(line.startXProperty()).multiply(3).divide(4)));

		final Point2D point = chart.getChartContentArea().sceneToLocal(initialSceneX, initialSceneY);
		start.setCenterY(point.getY());
		start.setCenterX(point.getX());
		end.setCenterX(point.getX());
		movePoint.setCenterX(point.getX());

		getChildren().addAll(start, line, movePoint, deletePoint, end);
	}

	public ControlPoint getEnd() {
		return end;
	}

	@Override
	public void update() {
		start.update();
	}
	
	@Override
	public ControlPoint[] getControlPoints() {
		return new ControlPoint[]{start,end, movePoint};
	}

}
